import {
    createKeybindingConfig,
    XFlowNodeCommands,
    XFlowEdgeCommands,
    XFlowGraphCommands,
    MODELS,
} from '@antv/xflow'

import type { NsNodeCmd, NsGraphCmd, NsEdgeCmd } from '@antv/xflow'

console.log('快捷键的id合集');
console.log(`删除边节点id`, XFlowNodeCommands.DEL_NODE.id, `删除边边id`, XFlowEdgeCommands.DEL_EDGE.id);
console.log(`复制`, XFlowGraphCommands.GRAPH_COPY.id);
console.log(`粘贴`, XFlowGraphCommands.GRAPH_PASTE.id);
console.log(`撤销`, XFlowGraphCommands.GRAPH_HISTORY_UNDO.id);
console.log(`回退`, XFlowGraphCommands.GRAPH_HISTORY_REDO.id,);

export const useKeybindingConfig = createKeybindingConfig(config => {
    // delete
    config.setKeybindingFunc(regsitry => {
        return regsitry.registerKeybinding([
            {
                id: 'delete node or edge',
                keybinding: 'backspace',
                callback: async function (item, modelService, cmd, e) {
                    const cells = await MODELS.SELECTED_CELLS.useValue(modelService)
                    cells.map(cell => {
                        if (cell.isNode()) {
                            return cmd.executeCommand<NsNodeCmd.DelNode.IArgs>(
                                XFlowNodeCommands.DEL_NODE.id,
                                {
                                    nodeConfig: {
                                        ...cell.getData(),
                                        id: cell.id,
                                    },
                                }
                            )
                        }
                        if (cell.isEdge()) {
                            return cmd.executeCommand<NsEdgeCmd.DelEdge.IArgs>(
                                XFlowEdgeCommands.DEL_EDGE.id,
                                {
                                    edgeConfig: { ...cell?.getData(), id: cell?.id },
                                }
                            )
                        }
                    })
                },
            },
            {
                id: 'copy',
                keybinding: ['command+c', 'ctrl+c'],
                callback: async function (item, modelService, cmd, e) {
                    e.preventDefault()
                    console.log(item)
                    cmd.executeCommand<NsGraphCmd.GraphCopySelection.IArgs>(
                        XFlowGraphCommands.GRAPH_COPY.id,
                        {},
                    )
                },
            },
            {
                id: 'paste',
                keybinding: ['command+v', 'ctrl+v'],
                callback: async function (item, ctx, cmd, e) {
                    e.preventDefault()
                    cmd.executeCommand<NsGraphCmd.GraphPasteSelection.IArgs>(
                        XFlowGraphCommands.GRAPH_PASTE.id,
                        {},
                    )
                },
            },
            {
                id: 'undo',
                keybinding: ['meta+z', 'ctrl+z'],
                callback: async function (item, ctx, cmd, e) {
                    e.preventDefault()
                    cmd.executeCommand<NsGraphCmd.GraphHistoryUndo.IArgs>(
                        XFlowGraphCommands.GRAPH_HISTORY_UNDO.id,
                        {},
                    )
                },
            },
            {
                id: 'redo',
                keybinding: ['meta+shift+z', 'ctrl+shift+z'],
                callback: async function (item, ctx, cmd, e) {
                    e.preventDefault()
                    cmd.executeCommand<NsGraphCmd.GraphHistoryRedo.IArgs>(
                        XFlowGraphCommands.GRAPH_HISTORY_REDO.id,
                        {},
                    )
                },
            },
        ])
    })
})